package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"
	"GameServer/Game/union"
	"encoding/json"
	"fmt"

	log "github.com/sirupsen/logrus"
)

//  公会发送红包
func (p *Player) UnionSendRedPacketReq(packId int64) {
	utils.AsyncFunc(func() {
		us := p.User

		ext := &m.ChatExt{Type: 3, LangId: data.LangID.UnionRedPack, Args: []string{us.Name}}
		_, err := union.UnionModule.UnionSendRedPacketMsg(us, ext, BroadData)
		if err != nil {
			return
		}

		goods, err := lib.GetRedPacketGoods(packId)
		if err != nil {
			return
		}
		// 100个钻石
		ext = &m.ChatExt{Type: 4, Goods: goods}
		msgChat, err := union.UnionModule.UnionSendRedPacket(us, ext, BroadData)
		if err != nil {
			return
		}

		redPacket := m.GetUnionRedPacket(us.Uid.Hex())
		redPacket.AddRedPacket(msgChat)
	})
}

// 1021 公会，玩家领取红包
func (p *Player) UnionRecvRedPacketRequest(req *sp.UnionRecvRedPacketRequest) (res *sp.UnionRecvRedPacketResponse) {
	us := p.User
	uid := us.Uid.Hex()
	tuid := req.Uid
	mid := req.Mid

	redPacket := m.GetUnionRedPacket(uid)
	// 红包是否领取过
	if redPacket.IsRecv(mid) {
		err := errors.NewErrcode(data.Errcode.ServerErr, fmt.Sprintf("已经领取过红包,mid:%s", mid))
		res = &sp.UnionRecvRedPacketResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// // 获取发红包的红包信息
	// tRedPacket := m.GetUnionRedPacket(tuid)
	// msgChat, ok := tRedPacket.GetSendRedPacket(mid)
	// if !ok {
	// 	err := errors.NewErrcode(data.Errcode.ServerErr, fmt.Sprintf("玩家tuid:%s没有发送红包,mid:%s", tuid, mid))
	// 	res = &sp.UnionRecvRedPacketResponse{
	// 		Errcode: errors.ErrCode(err),
	// 	}
	// 	return
	// }
	// 获取公会聊天信息
	chatMsg, err := lib.FindChatMsg(us, tuid, mid)
	if err != nil {
		res = &sp.UnionRecvRedPacketResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	cMsg := new(m.ChatExt)
	err = json.Unmarshal([]byte(chatMsg.Ext), &cMsg)
	if err != nil {
		log.WithFields(log.Fields{
			"data":    "解析消息失败",
			"chatMsg": chatMsg,
			"method":  "UnionRecvRedPacketRequest",
		}).Error(err)
		res = &sp.UnionRecvRedPacketResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// 用户领取红包中的道具
	uProp, resList, boxResList, err := lib.GiveGoodsByKvList("UnionRecvRedPacketRequest", us, cMsg.Goods, p.Send)
	if err != nil {
		res = &sp.UnionRecvRedPacketResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})

	recv := &m.GetRedPacket{
		Mid:      mid,
		RecvTime: utils.TNow().Unix(),
	}
	// 记录用户领取的红包
	redPacket.AddRecvRedPacket(recv)

	res = &sp.UnionRecvRedPacketResponse{
		Res:    lib.FormatGoodsInfoSps(resList),
		BoxRes: lib.FormatBoxResSps(boxResList),
	}
	return
}

// 1022 公会  玩家领取过的红包信息
func (p *Player) UnionRecvRedPacketInfoRequest(req *sp.UnionRecvRedPacketInfoRequest) (res *sp.UnionRecvRedPacketInfoResponse) {
	us := p.User
	redPacket, _ := lib.HandleRedPacket(us)

	res = &sp.UnionRecvRedPacketInfoResponse{
		Lst: lib.FormatRecvRedPacketInfoSps(redPacket.RecvRedPacket),
	}
	return
}
